package com.mys.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.portlet.ModelAndView;

import com.mys.service.GetData;
import com.mys.shorturl.GetShortUrlDao;


@Controller
@RequestMapping("/geturl")
public class GetShortUrlController {
	
	
	@Resource(name = "getShortUrlDao")
	private GetShortUrlDao getShortUrlDao;
	
	@Resource(name = "getData")
	private GetData getData;
	
	//@RequestMapping(value="/getshorturl",method = RequestMethod.GET)
	@RequestMapping(value="/getshorturl",method = RequestMethod.GET)
	@ResponseBody
	public String geturl(@RequestParam("longUrl") String longUrl,HttpServletRequest request){
		
		String urlbean = getShortUrlDao.getShortUrl(request,longUrl);
		//ModelMap modelMap = new ModelMap();
		//modelMap.addAttribute("urlbean", urlbean);
		return urlbean;
		
	}
	
	
	@RequestMapping(value="/getmyurl",method = RequestMethod.GET)
	@ResponseBody
	public ModelMap geturl(HttpServletRequest request){
		
		String urlbean = getShortUrlDao.getMyurl(request);
		
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("urlbean", urlbean);
		return modelMap;
		
	}
	
	@RequestMapping(value="/add",method = RequestMethod.POST)
	public String addUrl(@RequestParam("shortUrl") String shortUrl,@RequestParam("longUrl") String longUrl){
		getData.add(shortUrl, longUrl);
		return "index";
	}
	
	
}
